package letcode.problem.dynamicProgra;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 最长数对链
 * https://leetcode.cn/problems/maximum-length-of-pair-chain/description/?envType=study-plan-v2&envId=dynamic-programming
 * */
public class MaximumLengthOfPairChain {
    public static int findLongestChain(int[][] pairs) {
        int m = pairs.length;
        Arrays.sort(pairs, Comparator.comparingInt(a -> a[0]));
        int[] dp = new int[m];
        int maxLen = 1;
        for (int i = 0; i < m; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (isBigger(pairs[j], pairs[i])) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            maxLen = Math.max(dp[i], maxLen);
        }
        return maxLen;
    }

    public static boolean isBigger(int[] left, int[] right) {
        return right[0] > left[1];
    }
}
